package code05_方法;

import java.util.Scanner;

public class 最后一题_2 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("请选择年份：");
		int year = input.nextInt();
		System.out.print("请选择月份：");
		int month = input.nextInt();
		System.out.println("输入年份距离1900年1月1日的天数：" + dayNum(year));
		System.out.println("输入月份距离1900年1月1日的天数："
				+ (dayNum(year) + monthDay(year, month)));
		System.out.println("当前月份的天数:" + day(year, month));
	}
	
	// 算出距离1900天数
	public static int dayNum(int year) {
		int day = 0;
		if (year > 1900) { // 1900年之后的年份
			for (int i = 1900; i < year; i++) {
				day += yearDay(i);
			}
		}else{ // 1900年之前的年份
			for (int i = 1900; i > year; i--) {
				day += yearDay(i);
			}
		}
		return day;
	}
	
	// 距离月的天数
	public static int monthDay(int year, int month) {
		int february = leap(year) ? 29 : 28;
		int[] months = { 31, february, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		
		int days = 0;
		for (int i = 0; i < month - 1; i++) {
			days += months[i];
		}
		return days;
	}
	
	// 当前年(其实是上一年)的天数
	public static int yearDay(int year) {
		if (leap(year)) {
			return 366;
		} else {
			return 365;
		}
	}
	
	// 当前月份天数
	public static int day(int year, int month) {
		int february = leap(year) ? 29 : 28;
		int[] months = { 31, february, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		return months[month-1];
	}

	// 判断平闰年
	public static boolean leap(int year) {
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
			return true;
		} else {
			return false;
		}
	}
}
